﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class TransparentManager : MonoBehaviour {

	public GameObject [] transObjects;
	GameObject cameraRoot;
	List<GameObject> transObjList = new List<GameObject>();
	List<ToggleSemiTransparent> transList = new List<ToggleSemiTransparent>();

	void Start()
	{
		transObjList = transObjects.ToList();
		foreach(GameObject go in transObjList)
			transList.Add ( go.GetComponent<ToggleSemiTransparent>() );
	}

	void Update()
	{
		if(BattleManager.Instance.isBattleStarted && transObjects.Length>0) {

			cameraRoot=BattleManager.Instance.cameraRoot;

			foreach(ToggleSemiTransparent go in transList)
				go.ResetTrans();
			
			RaycastHit hitinfo;
			//Debug.DrawLine(cameraRoot.transform.position, cameraRoot.transform.position + cameraRoot.transform.forward *10);
			if (Physics.Raycast(cameraRoot.transform.position, cameraRoot.transform.forward, out hitinfo, 20, 1 << LayerMask.NameToLayer("BlockingObject"))) {
				//Debug.LogError("There is something in front of the object!");
				int i=0;
				foreach(GameObject go in transObjList) {
					if(go==hitinfo.collider.gameObject)
						transList[i].SetTrans();
					i++;
				}
			}
		}
	}
}
